package duylinh

import org.springframework.dao.DataIntegrityViolationException

class VatLieuController {
    def utilsService
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {

        params.max = Math.min(max ?: 10, 100)
        [vatLieuInstanceList: VatLieu.list(params), vatLieuInstanceTotal: VatLieu.count()]
    }


    def createEditVatLieuFlow = {

        start{
            action{
                def vatLieu

                if(params.vatLieuId)
                {
                    vatLieu = VatLieu.findById( params.vatLieuId )
                    flow.isNewVatLieu = false
                }
                else
                {
                    vatLieu = new VatLieu()
                    flow.isNewVatLieu = true
                }
                flow.vatLieu = vatLieu
            }
            on("success").to("basicInfo")
        }

        basicInfo{
            on("tiep"){

                flow.vatLieu.properties = params

                println("vatlieu name: " + flow.vatLieu.ten)
                println("vatlieu avatarUrl: " + flow.vatLieu.avatarUrl)
                println("vatlieu params.avatarUrl: " + params.avatarUrl)

                def user = User.get(springSecurityService.principal.id)
                if( user )
                {
                    if ( flow.isNewVatLieu )
                    {
                        flow.vatLieu?.creator = user
                    }
                    else
                    {
                        flow.vatLieu?.updater = user
                    }
                }
                println("user: " + user)

                println("flow.vatLieu?.creator: " + flow.vatLieu?.creator)
                println("flow.vatLieu?.updater: " + flow.vatLieu?.updater)

                if(!flow.vatLieu.validate())
                {
                    return error()
                }
                else
                {
                    flow.vatLieu.save(flush:true)
                    utilsService.saveVatLieuAvatar( flow.vatLieu, params.avatarUrl )

                }

            }.to("chonHoacTaoNhaCungUng")
            on("huy").to ("thoatFlow")
        }

        chonHoacTaoNhaCungUng{
            action{
                if( NhaCungUng.count() > 0 )
                {
                    return chonNhaCungUng()
                } else
                {
                    return taoNhaCungUng()
                }
            }
            on("chonNhaCungUng").to "chonNhaCungUng"
            on("taoNhaCungUng"){

            }.to "taoNhaCungUng"
            on(Exception).to "basicInfo"
        }

        chonNhaCungUng{
            on("chonNhaCungUng")
            {
                println("params: " + params)
                flow.vatLieu.nhaCungUng = NhaCungUng.get(Long.parseLong(params.nhaCungUng))
                if( !flow.vatLieu.validate() )
                {
                    return error()
                }
                else
                {
                    flow.vatLieu.save(flush:true)
                }

            }.to("thoatFlow")
            on("suaNhaCungUng").to("taoNhaCungUng")
            on("taoNhaCungUng").to("taoNhaCungUng")
        }

        taoNhaCungUng{
            on("tiep"){
                def nhaCungUng

                println("params: "+params)

                println("params: "+params.nhaCungUng)

                if( params.nhaCungUng )
                {
                    nhaCungUng = NhaCungUng.findById(params.nhaCungUng?.id)
                    flow.isNewNhaCungUng = false
                }
                else
                {
                    nhaCungUng = new NhaCungUng(params)
                    flow.isNewNhaCungUng = true
                }

                def user = User.get(springSecurityService.principal.id)
                if( user )
                {
                    if ( flow.isNewNhaCungUng )
                    {
                       nhaCungUng?.creator = user
                    }
                    else
                    {
                       nhaCungUng?.updater = user
                    }
                }
                println("user: " + user)

                if( !nhaCungUng.validate() )
                {
                    return error()
                }
                else
                {
                    nhaCungUng.save(flush:true)
                    flow.vatLieu.nhaCungUng = nhaCungUng
                    flow.vatLieu.save(flush:true)
                }
            }.to("thoatFlow")

            on("huy").to("thoatFlow")
            on("quaylai").to("basicInfo")
        }

        thoatFlow{
            redirect(action: "list")
        }
    }



    def create() {
        [vatLieuInstance: new VatLieu(params)]
    }

    def save() {
        def vatLieuInstance = new VatLieu(params)
        if (!vatLieuInstance.save(flush: true)) {
            render(view: "create", model: [vatLieuInstance: vatLieuInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'vatLieu.label', default: 'VatLieu'), vatLieuInstance.id])
        redirect(action: "show", id: vatLieuInstance.id)
    }

    def show(Long id) {
        def vatLieuInstance = VatLieu.get(id)
        if (!vatLieuInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vatLieu.label', default: 'VatLieu'), id])
            redirect(action: "list")
            return
        }

        [vatLieuInstance: vatLieuInstance]
    }

    def edit(Long id) {
        def vatLieuInstance = VatLieu.get(id)
        if (!vatLieuInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vatLieu.label', default: 'VatLieu'), id])
            redirect(action: "list")
            return
        }

        [vatLieuInstance: vatLieuInstance]
    }

    def update(Long id, Long version) {
        def vatLieuInstance = VatLieu.get(id)
        if (!vatLieuInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vatLieu.label', default: 'VatLieu'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (vatLieuInstance.version > version) {
                vatLieuInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'vatLieu.label', default: 'VatLieu')] as Object[],
                        "Another user has updated this VatLieu while you were editing")
                render(view: "edit", model: [vatLieuInstance: vatLieuInstance])
                return
            }
        }

        vatLieuInstance.properties = params

        if (!vatLieuInstance.save(flush: true)) {
            render(view: "edit", model: [vatLieuInstance: vatLieuInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'vatLieu.label', default: 'VatLieu'), vatLieuInstance.id])
        redirect(action: "show", id: vatLieuInstance.id)
    }

    def delete(Long id) {
        def vatLieuInstance = VatLieu.get(id)
        if (!vatLieuInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vatLieu.label', default: 'VatLieu'), id])
            redirect(action: "list")
            return
        }

        try {
            vatLieuInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'vatLieu.label', default: 'VatLieu'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'vatLieu.label', default: 'VatLieu'), id])
            redirect(action: "show", id: id)
        }
    }
}
